package com.hl.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

import com.hl.domain.CouponProductList;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hl.domain.CouponProduct;
import com.hl.mapper.CouponMapper;
import com.hl.domain.Coupon;
import com.hl.service.ICouponService;

/**
 * 优惠券Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-08
 */
@Service
public class CouponServiceImpl implements ICouponService 
{
    @Autowired
    private CouponMapper couponMapper;

    /**
     * 查询优惠券
     * 
     * @param id 优惠券主键
     * @return 优惠券
     */
    @Override
    public Coupon selectCouponById(String id)
    {
        return couponMapper.selectCouponById(id);
    }

    /**
     * 查询优惠券列表
     * 
     * @param coupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<Coupon> selectCouponList(Coupon coupon)
    {
        return couponMapper.selectCouponList(coupon);
    }

    /**
     * 新增优惠券
     * 
     * @param coupon 优惠券
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCoupon(Coupon coupon)
    {
//       CouponProduct.setCouponId(coupon.getId());

        coupon.setCreateTime(DateUtils.getNowDate());
        //判断当前时间是否大于优惠券的开始时间，如果大于则将优惠券的状态设置为进行中
        Date startTime = coupon.getStartTime();
        LocalDateTime startDateTime = startTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime now = LocalDateTime.now();
        if (now.isAfter(startDateTime)) {
            coupon.setStatus("ACTIVE");
        }else {
            coupon.setStatus("CREATED");
        }
        int rows = couponMapper.insertCoupon(coupon);
        System.out.println("Coupon ID: " + coupon.getId());
        System.out.println(rows+"rows");
        insertCouponProduct(coupon);
        return rows;
    }

    /**
     * 修改优惠券
     * 
     * @param coupon 优惠券
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCoupon(Coupon coupon)
    {
        coupon.setUpdateTime(DateUtils.getNowDate());
        couponMapper.deleteCouponProductByCouponId(coupon.getId());
        insertCouponProduct(coupon);
        return couponMapper.updateCoupon(coupon);
    }

    /**
     * 批量删除优惠券
     * 
     * @param ids 需要删除的优惠券主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCouponByIds(String[] ids)
    {
        couponMapper.deleteCouponProductByCouponIds(ids);
        return couponMapper.deleteCouponByIds(ids);
    }

    /**
     * 删除优惠券信息
     * 
     * @param id 优惠券主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCouponById(String id)
    {
        couponMapper.deleteCouponProductByCouponId(id);
        return couponMapper.deleteCouponById(id);
    }

    /**
     * 新增优惠券-商品关联（scope_type=PRODUCT时生效）信息
     * 
     * @param coupon 优惠券对象
     */
    public void insertCouponProduct(Coupon coupon)
    {
        List<CouponProductList> couponProductList = coupon.getCouponProductLists();
        String id = coupon.getId();
        if (StringUtils.isNotNull(couponProductList))
        {
            List<CouponProduct> list = new ArrayList<CouponProduct>();
            for (CouponProductList couponProduct1 : couponProductList)
            {
                CouponProduct couponProduct = new CouponProduct();
                couponProduct.setProductId(Long.valueOf(couponProduct1.getProductId()));
                couponProduct.setCreateTime(couponProduct1.getCreateTime());
                couponProduct.setCouponId(id);
                System.out.println(couponProduct+"couponProduct");
                list.add(couponProduct);
            }
            if (list.size() > 0)
            {
                couponMapper.batchCouponProduct(list);
            }
        }
    }
}
